// myPages/teacherCenter/teacherCenter.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    teacherInfo:[],  //导师个人信息
    if_open_talk : '',  //是否开放约见：1为是 0为否
    serverPoup: false,
    show:false,
  },

  // 查询导师个人信息
  getUserInfoData() {
    var that = this;
    var teacherId = wx.getStorageSync('userInfo').id //获取老师id
    util.request(api.UserInfo, { userId: teacherId}, 'POST', false).then(function (res) {
      console.log(res)
      if (res.code === 200) {
        that.setData({
          teacherInfo : res.result[0],
          if_open_talk : res.result[0].isOpenTalk == 1 ? true : false
        })
      }
    });
  },

  //开放预约点击事件
  openTalkChange(){
    var that = this;
    that.setData({
      if_open_talk : that.data.if_open_talk == true ? false : true
    })
    let tutor = {
      id: that.data.teacherInfo.id,
      isOpenTalk: that.data.if_open_talk == true ? 1 : 0
    }
    util.request(api.UpdateTutorInfo, tutor, 'POST', true).then(function (res) {
      console.log(res)
      if (res.code === 200) {
        console.log("change success!")
      }
    });
  },

  //加载导师手册pdf
  gotoTeacherBook(){
    wx.downloadFile({
      url: 'https://knowif.oss-cn-beijing.aliyuncs.com/tutorPicture/pdf%E6%96%87%E4%BB%B6/%E5%AF%BC%E5%B8%88%E6%89%8B%E5%86%8C.pdf',      //要预览的PDF的地址
      success: function (res) {
        console.log(res);
        if (res.statusCode === 200) {                     //成功
          var Path = res.tempFilePath                     //返回的文件临时地址，用于后面打开本地预览所用
          wx.openDocument({
            filePath: Path,                               //要打开的文件路径
            success: function (res) {
              console.log('打开PDF成功');
            }
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });                                 
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //接收传过来的参数
    console.log('接收到的teacherId为：' + options.teacherid);
  },
  /**客服反馈 */
  gotoCustomerService() {
    // wx.navigateTo({
    //   url: '../../myPages/customerService/customerService'
    // })
    this.setData({
      show: true,
      serverPoup: true
    })
  },
  /*关闭弹框*/
  onClose() {
    this.setData({
      show: false,
      serverPoup: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfoData();
    wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})